package market

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/backend_controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/market"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/market/outbound_task"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type BackendOutboundTaskController struct {
	backend_controller.BackendBaseController
	outboundTaskService outbound_task.OutboundTaskServiceIface
}

// NewBackendOutboundTaskController 外呼任务
func NewBackendOutboundTaskController(
	outboundTaskService outbound_task.OutboundTaskServiceIface,
) *BackendOutboundTaskController {
	return &BackendOutboundTaskController{
		outboundTaskService: outboundTaskService,
	}
}

func (c *BackendOutboundTaskController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("outbound/task/list", c.taskList)
	g.POST("outbound/clue/list", c.clueList)
	g.POST("outbound/clue/audit", c.audit)
	g.GET("outbound/clue/detail", c.detail)
	g.GET("outbound/clue/tenant-with-outbound-profit", c.tenantWithOutboundProfit)
}

func (c *BackendOutboundTaskController) taskList(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.GetTaskListRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.TenantCode == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
		return
	}
	ctx.Set(authorize.TENANT_CODE_KEY, request.TenantCode)

	companyId, err := utils.GetByCompanyId(ctx, request.TenantCode)
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.GetTaskList(ctx, companyId, 0, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "http taskList GetTaskList request:%+v, err:%s", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *BackendOutboundTaskController) clueList(ctx *gin.Context) {
	var req dto.GetOutboundClueListRequest
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}
	if req.TenantCode == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
		return
	}
	ctx.Set(authorize.TENANT_CODE_KEY, req.TenantCode)

	resp, err := c.outboundTaskService.GetOutboundClueList(ctx, &req)
	if err != nil {
		stark.Logger.Errorf(ctx, "http clueList GetOutboundClueList request:%+v, err:%s", req, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, resp)
}

func (c *BackendOutboundTaskController) audit(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.AuditOutboundClueRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	if request.TenantCode == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
		return
	}
	ctx.Set(authorize.TENANT_CODE_KEY, request.TenantCode)

	companyId, err := utils.GetByCompanyId(ctx, request.TenantCode)
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}

	ztUser := ctx.Value("ZhanTuUserName")
	if ztUser != nil {
		if ztUser.(string) != "" {
			request.AuditUserName = ztUser.(string)
		}
	}

	// 2，逻辑处理
	err = c.outboundTaskService.AuditOutboundClue(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "http audit AuditOutboundClue request:%+v, err:%s", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, map[string]interface{}{"is_success": true})
}

func (c *BackendOutboundTaskController) detail(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.GetOutboundClueDetailRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	if request.TenantCode == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
		return
	}
	ctx.Set(authorize.TENANT_CODE_KEY, request.TenantCode)

	companyId, err := utils.GetByCompanyId(ctx, request.TenantCode)
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}

	// 2，逻辑处理
	resp, err := c.outboundTaskService.GetOutboundClueDetail(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "http clueDetail GetOutboundClueDetail request:%+v, err:%s", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, resp)
}

func (c *BackendOutboundTaskController) tenantWithOutboundProfit(ctx *gin.Context) {
	tenantCode, err := utils.GetAllTenantCode(ctx)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	if len(tenantCode) == 0 {
		c.Success(ctx, []string{})
	}

	c.Success(ctx, tenantCode)
}
